package com.pay.ioopos.support.serialport.custom;

import com.pay.ioopos.support.serialport.CmdPackException;
import com.pay.ioopos.support.utils.BigDecimalUtils;

import java.nio.charset.StandardCharsets;

/**
 * 与发起收款指令有的关利用代码块
 * @author moyq5
 * @since 2022/8/2
 */
public final class SerialPortPayCmd4SubmitDerUtils {

    private SerialPortPayCmd4SubmitDerUtils() {}

    public static SerialPortPayCmd4Submit deserialize(SerialPortPayCmd4Submit result) {
        byte[] content = result.getContent();
        // 金额，3个字节，高位在前，单位：分
        int fen = (((content[0] & 0xFF) << 16) | ((content[1] & 0xFF) << 8) | (content[2] & 0xFF));
        result.setAmount(BigDecimalUtils.fenToYuan(fen).toString());
        // 单号长度
        int length = content[3] & 0xFF;
        if (length == 0) {
            return result;
        }
        if (length > content.length - 3) {
            throw new CmdPackException("单号长度有误：" + length);
        }
        // 单号
        byte[] bytes = new byte[length];
        System.arraycopy(content, 4, bytes, 0, length);
        result.setOrderNo(new String(bytes, StandardCharsets.UTF_8));
        return result;
    }

}
